package com.shequan.shequan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ailk.dazzle.util.data.JSONUtils;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.common.bean.ViewTextConstans;
import com.shequan.common.bean.WebConstants;
import com.shequan.shequan.bean.bo.FamousShequanDetail;
import com.shequan.shequan.bean.bo.QryShequanMember;
import com.shequan.shequan.bean.bo.SqActive;
import com.shequan.shequan.bean.pojo.Shequan;
import com.shequan.shequan.bean.pojo.ShequanType;
import com.shequan.shequan.service.ShequanJoinApproveService;
import com.shequan.shequan.service.ShequanService;
import com.shequan.shequan.service.ShequanTypeService;
import com.shequan.shequan.service.SqActivityService;
import com.shequan.user.bean.bo.UserSession;
import com.shequan.user.bean.pojo.UserInfo;
import com.shequan.user.service.UserInfoService;

/**
 * <P>
 * Copyright 2012.
 * </p>
 * <P>
 * All Rights Reserved.
 * </p>
 * 
 * @author YuJF
 * @version 1.0, 2012-3-8
 * @see com.shequan.shequan.controller.ShequanController
 * @since 1.0
 */
@Controller
@RequestMapping("/shequan")
public class ShequanController {
	private static final Logger log = Logger.getLogger(ShequanController.class);

	private SqActivityService sqActivityService;
	private ShequanTypeService shequanTypeService;
	private ShequanJoinApproveService shequanJoinApproveService;
	private UserInfoService userInfoService;
	private ShequanService shequanService;

	/**
	 * @param shequanService
	 *            the shequanService to set
	 */
	public void setShequanService(ShequanService shequanService) {
		this.shequanService = shequanService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public void setShequanJoinApproveService(ShequanJoinApproveService shequanJoinApproveService) {
		this.shequanJoinApproveService = shequanJoinApproveService;
	}

	public void setShequanTypeService(ShequanTypeService shequanTypeService) {
		this.shequanTypeService = shequanTypeService;
	}

	public void setSqActivityService(SqActivityService sqActivityService) {
		this.sqActivityService = sqActivityService;
	}

	@RequestMapping(value = "/index")
	public ModelAndView getShequanActivityMsg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("---in getShequanAcitvityMsg---");

		ResultDataStruct<SqActive> rds = null;
		try {
			Integer shequanId = Integer.parseInt(request.getParameter("shequanId"));
			System.out.println(shequanId);
			rds = sqActivityService.shequanAcitiveMsg(shequanId);
			if (rds.isBusiSuccess()) {
				request.setAttribute("sqActive", rds.getData());
			}
		} catch (Exception e) {
			if (rds == null) {
				rds = new ResultDataStruct<SqActive>();
			}
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		return new ModelAndView("/pages/shequan/shequan.jsp");
	}

	/**
	 * 查询所有社圈类型
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/types")
	public void getShequanType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<ShequanType>> rds = shequanTypeService.queryAllShequanType();
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}

	@RequestMapping(value = "/join")
	public void joinShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String shequanIds = request.getParameter("shequan");
		UserSession userSession = (UserSession) request.getSession().getAttribute(WebConstants.USER_SESSION);
		String userName = userSession.getUserName();
		ResultDataStruct<String> rds = null;
		UserInfo userInfo;
		try {
			userInfo = userInfoService.queryUserInfoByUserName(userName);
			rds = shequanJoinApproveService.addUserJoinApprove(shequanIds, userInfo);
		} catch (Exception e) {
			if (rds == null) {
				rds = new ResultDataStruct<String>();
			}
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-4-15 上午9:45:00
	 */
	@RequestMapping(value = "/getMember")
	public void getMember(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<QryShequanMember> rds = new ResultDataStruct<QryShequanMember>();
		int shequanId = Integer.parseInt(request.getParameter("shequanId"));
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		try {

			QryShequanMember shequanMemberList = shequanService.queryShequanMemberByPage(shequanId, page, limit);
			rds.setData(shequanMemberList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-4-15 上午9:44:56
	 */
	@RequestMapping(value = "/friendShequan")
	public void getFriendShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<QryShequanMember> rds = new ResultDataStruct<QryShequanMember>();
		int shequanId = Integer.parseInt(request.getParameter("shequanId"));
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		try {

			QryShequanMember shequanMemberList = shequanService.queryShequanMemberByPage(shequanId, page, limit);
			rds.setData(shequanMemberList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-4-15 上午9:44:49
	 */
	@RequestMapping(value = "/modifyFriendShequan")
	public void modifyFriendShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<QryShequanMember> rds = new ResultDataStruct<QryShequanMember>();
		int shequanId = Integer.parseInt(request.getParameter("shequanId"));
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		try {

			QryShequanMember shequanMemberList = shequanService.queryShequanMemberByPage(shequanId, page, limit);
			rds.setData(shequanMemberList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-4-15 上午9:44:49
	 */
	@RequestMapping(value = "/famous/all")
	public void allFamousShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<FamousShequanDetail>> rds = new ResultDataStruct<List<FamousShequanDetail>>();
		try {

			List<FamousShequanDetail> shequanMemberList = shequanService.queryAllFamousShequanDetail();
			rds.setData(shequanMemberList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
			log.error(e);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	@RequestMapping(value = "/shequan/all")
	public void allShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<Shequan>> rds=new ResultDataStruct<List<Shequan>>();
		String shequanType=request.getParameter("shequanType");
		log.debug("shequanType:"+shequanType);
		if(shequanType==null){
			
		}else{
			
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		response.getWriter().print(jsonData);
	}
}
